package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseRepairTypeAddBo;
import com.manage.modules.bo.TBaseRepairTypeQueryBo;
import com.manage.modules.bo.TBaseRepairTypeEditBo;
import com.manage.modules.domain.TBaseRepairType;
import com.manage.modules.mapper.TBaseRepairTypeMapper;
import com.manage.modules.vo.TBaseRepairTypeVo;
import com.manage.modules.service.ITBaseRepairTypeService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 维修类型Service业务层处理
 *
 * @author wx
 * @date 2021-08-17
 */
@Service
public class TBaseRepairTypeServiceImpl extends ServiceImpl<TBaseRepairTypeMapper, TBaseRepairType> implements ITBaseRepairTypeService {

    @Override
    public TBaseRepairTypeVo queryById(Long repairTypeId) {
        TBaseRepairType db = this.baseMapper.selectById(repairTypeId);
        return BeanUtil.toBean(db, TBaseRepairTypeVo.class);
    }

    @Override
    public List<TBaseRepairTypeVo> queryList(TBaseRepairTypeQueryBo bo) {
        LambdaQueryWrapper<TBaseRepairType> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getRepairTypeName()), TBaseRepairType::getRepairTypeName, bo.getRepairTypeName());
        lqw.eq(bo.getRepairTypeState() != null, TBaseRepairType::getRepairTypeState, bo.getRepairTypeState());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseRepairTypeVo> entity2Vo(Collection<TBaseRepairType> collection) {
        List<TBaseRepairTypeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseRepairTypeVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseRepairType> page = (Page<TBaseRepairType>) collection;
            Page<TBaseRepairTypeVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseRepairTypeAddBo bo) {
        TBaseRepairType add = BeanUtil.toBean(bo, TBaseRepairType.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseRepairTypeEditBo bo) {
        TBaseRepairType update = BeanUtil.toBean(bo, TBaseRepairType.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseRepairType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
